package com.joy.openggl;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;

import com.joy.openggl.graphics.Graphics;
import com.joy.openggl.graphics.Image;
import com.joy.openggl.graphics.mesh.Cube;
import com.joy.openggl.graphics.simple.SimplePlane;
import com.joy.openggl.util.BitmapUtil;

public class OpenGLRenderer implements Renderer {
	private Cube cube;
	private float angle;
	
	private Square square;
	private SimplePlane SimplePlane;
	
	private Image img;
	
	private void render(GL10 gl) {
//		if (cube == null) {
//		cube = new Cube(1.0f, 1.0f, 1.0f);
//	}
//	angle ++;
//	cube.setRotatef(angle, angle, 0);
//	cube.draw(gl);
		
		if(square == null) {
			square = new Square();
		}
		square.draw(gl);
		
		if(SimplePlane == null) {
			SimplePlane = new SimplePlane(1.0f, 1.0f);
			SimplePlane.loadBitmap(BitmapUtil.makeBitmapFromAssets("face_girl.png"));
		}
		SimplePlane.draw(gl);
		
		if(img == null) {
			img = Image.create(BitmapUtil.makeBitmapFromAssets("icon_boss.png"));
		}
		img.draw(gl, -0.2f, -0.2f, 0, 0, 224, 313);
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

		gl.glLoadIdentity();
		gl.glTranslatef(0, 0, -5.0f);
		
		render(gl);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		gl.glViewport(0, 0, width, height);// 指定 Viewport 变换
		// Select the projection matrix
		gl.glMatrixMode(GL10.GL_PROJECTION);// •选择当前矩阵种类(GL10.GL_PROJECTION:指定投影变换)
		// Reset the projection matrix
		gl.glLoadIdentity();// 将当前矩阵设为单位矩阵
		// Calculate the aspect ratio of the window
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);// 同时指定viewing matrix 和modeling matrix.
		// Reset the modelview matrix
		gl.glLoadIdentity();// OpenGL docs.

		gl.glOrthof(0, width, height, 0, -1, 1);
		// gl.glEnable(GL10.GL_BLEND);
		// gl.glEnable(GL10.GL_TEXTURE_2D);
		// gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		// gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		Graphics.gl = gl;
		
		// Set the background color to black ( rgba ).
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // OpenGL docs.
		// Enable Smooth Shading, default not really needed.
		gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs.
		// Depth buffer setup.
		// gl.glClearDepthf(1.0f);// OpenGL docs.
		// Enables depth testing.
		// gl.glEnable(GL10.GL_DEPTH_TEST);// OpenGL docs.
		// The type of depth testing to do.
		// gl.glDepthFunc(GL10.GL_LEQUAL);// OpenGL docs.
		// Really nice perspective calculations.
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, // OpenGL docs.
				GL10.GL_NICEST);

		// 关闭抗抖动
		gl.glDisable(GL10.GL_DITHER);
		// 关闭深度测试
		gl.glDisable(GL10.GL_DEPTH_TEST);

	}

}
